#!/usr/bin/perl
#use strict;
#use warnings;
use File::stat;
use File::Basename;

### global variables ###

@warnings = ();

### main ###

if (system('mkdir renamed')) {
    print_error('cannot mkdir') if (!-d 'renamed');
};

my $rename_only = 0;
my $pretend = 0;
my $file_opreation = 'cp -rpf';
get_options();
#my $jar_file = File::Basename::dirname($0) . '/' . $jar_file_name;
#%genres = ();*/
recode_tracks();

### procedures ###

sub recode_tracks {
    if (!opendir(DIR_HANDLE, '.')) {
        print_error('Cannot open current directory');
    };
    print "Recoding tracks...\n";
    foreach my $entry (sort readdir(DIR_HANDLE)) {
        my $quoted_entry = $entry;
        quote_spec_chars(\$quoted_entry);
        next if ($entry =~ /^\.+$/);
        next if (-d $entry);
        my $new_name = $entry;

        open(FILEINFO, "file \"$entry\" |");
        $info_string = <FILEINFO>;
        close FILEINFO;
        chomp($info_string);
        if ($info_string !~ /\s+image\sdata/) {
            print "$entry is not an image, skipping\n";
            next;
        };


        open(FILEDATE, "strings \"$entry\" | grep '^[0-9][0-9][0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]' | uniq | sort | head -n 1 |");
        $date_string = <FILEDATE>;
        close FILEDATE;
        if ($date_string == NULL) {
            print("Cannot digest \"$entry\". Please check the filename.\n");
            next;
        };

        chomp($date_string);
        $date_string =~ s/\s+/_/g;
        $date_string =~ s/\://g;

        my $ext = lc((fileparse($entry, qr/\.[^.]*/))[2]);

        $new_name = 'renamed/' . $date_string . $ext;
        print "\n";
        system "ls \"$entry\"";
        quote_spec_chars(\$entry);

        if ($pretend) {
            print("$file_opreation \"$entry\" $new_name");
        } else {
            run_system_call("$file_opreation \"$entry\" $new_name");
        };
    };
    closedir DIR_HANDLE;
    exit;
};

sub quote_spec_chars {
        my $string = shift;
        $$string =~ s/\(/\\\(/g;
        $$string =~ s/\)/\\\)/g;
        $$string =~ s/\'/\\\'/g;
        $$string =~ s/\s/\\ /g;
        $$string =~ s/\&/\\\&/g;
        $$string =~ s/\!/\\\!/g;
        #return $string;
};
sub get_options {
    while (my $arg = shift @ARGV) {
        if ($arg =~ /^--help$/ || $arg =~ /^-h$/) {
            print_usage();
        }; 
        if ($arg =~ /^--move$/ || $arg =~ /^-r$/) {
            $move_files++;
        }; 
        if ($arg =~ /^-p$/) {
            $pretend++;
        }; 
    };
    $file_opreation = 'mv' if ($move_files && !$convert_id3_tags);
};

sub print_usage {
    print STDERR "\nrecode_mp3\n";
    print STDERR "        Just start it, and you get the renamed files copied in the directory \"renamed\"\n";
    print STDERR "        --move     - move files instead of copying\n";
    print STDERR "        -p     - pretend, just show what is gonna be made\n";
    exit(0);
};

sub run_system_call {
    my $system_command = shift;
    if ($pretend) {
        print $system_command . "\n";
        return;
    };
    system($system_command);
};

sub print_error {
    my $message = shift;
    $message = "\nError:" . $message . "\n";
    print STDERR $message;
    exit(1);
};

sub print_warnings {
    return if (!scalar @warnings);
    foreach $warning (@warnings) {
        print STDERR "\nWARNING: $warning\n";
    };
};
